using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using SystemFrameworks;
using Microsoft.ApplicationBlocks.Data;
using Common; using MinCultura.Sipa.DataAccess.EntidadesArtes;
using SystemFrameworks;

namespace MinCultura.Sipa.EntidadesArtes
{
	/// <summary>
	/// Clase de acceso a datos para la entidad ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD
	/// </summary>
	public class AreasArtisticasFormacionEntidad 
	{
		#region Private Data Access Atributes

		private const String ENT_ID_PARAM = "@ENT_ID";
		private const String ARE_ID_PARAM = "@ARD_ID";

		#endregion Private Data Access Atributes

		/// <summary>
		/// Constructor de la clase ARTAREASARTISTICASFORMACIONENTIDAD
		/// </summary>
        public AreasArtisticasFormacionEntidad(string strCadenaConecxion)
        {
            ConnectionString = strCadenaConecxion;
        }

        private string _cadenaConexion;

        public string ConnectionString
        {
            get { return _cadenaConexion; }
            set { _cadenaConexion = value; }
        }

		public AreasArtisticasFormacionEntidadData BuscarAreasArtisticasFormacionEntidad()
		{
			AreasArtisticasFormacionEntidadData response = new AreasArtisticasFormacionEntidadData();
			SqlHelper.ExecuteDataTable(this.ConnectionString, CommandType.StoredProcedure, "PAS_BUSCAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD", response);
			if (response.Rows.Count == 0)
				return null;
			return response;
		}


		public AreasArtisticasFormacionEntidadData ConsultarAreasArtisticasFormacionEntidad(decimal parENT_ID, decimal parARE_ID)
		{
			AreasArtisticasFormacionEntidadData response = new AreasArtisticasFormacionEntidadData();
			SqlParameter param1 = new SqlParameter(ENT_ID_PARAM, parENT_ID);
			SqlParameter param2 = new SqlParameter(ARE_ID_PARAM, parARE_ID);
			SqlParameter[] parameters = {param1, param2};
			SqlHelper.ExecuteDataTable(this.ConnectionString, CommandType.StoredProcedure, "PAS_CONSULTAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD", response, parameters);
			if (response.Rows.Count == 0)
				return null;
			return response;
		}


		public AreasArtisticasFormacionEntidadData ConsultarAreasArtisticasFormacionEntidad(decimal parENT_ID)
		{
			AreasArtisticasFormacionEntidadData response = new AreasArtisticasFormacionEntidadData();
			SqlParameter param1 = new SqlParameter(ENT_ID_PARAM, parENT_ID);
			SqlParameter[] parameters = {param1};
			SqlHelper.ExecuteDataTable(this.ConnectionString, CommandType.StoredProcedure, "PAS_CONSULTAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD_ARD_ID", response, parameters);
			if (response.Rows.Count == 0)
				return null;
			return response;
		}

		public void ActualizarAreasArtisticasFormacionEntidad(AreasArtisticasFormacionEntidadData datos)
		{
			
			try
			{
				foreach (DataRow row in datos.Rows)
				{
					SqlParameter param1 = new SqlParameter(ENT_ID_PARAM, row[AreasArtisticasFormacionEntidadData.ENT_ID_FIELD]);
					SqlParameter param2 = new SqlParameter(ARE_ID_PARAM, row[AreasArtisticasFormacionEntidadData.ARE_ID_FIELD]);

					SqlParameter[] parameters = {param1, param2};
					SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ACTUALIZAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD", parameters);
				}
			}
			catch (SqlException sqlEx)
			{
				throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "PAS_ACTUALIZAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD"), sqlEx);
			}
			catch (Exception ex)
			{
				throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorActualizar", "PAS_ACTUALIZAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD"), ex);
			}
		}
		public void AdicionarAreasArtisticasFormacionEntidad(AreasArtisticasFormacionEntidadData datos, params SqlTransaction[] transaccion)
		{
			try
			{
				foreach (DataRow row in datos.Rows)
				{
					SqlParameter param1 = new SqlParameter(ENT_ID_PARAM, row[AreasArtisticasFormacionEntidadData.ENT_ID_FIELD]);
					SqlParameter param2 = new SqlParameter(ARE_ID_PARAM, row[AreasArtisticasFormacionEntidadData.ARE_ID_FIELD]);
					SqlParameter[] parameters = {param1, param2};
					if (transaccion.Length == 0)
					{
						SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ADICIONAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD", parameters);
					}
					else
					{
						SqlHelper.ExecuteNonQuery(transaccion[0], CommandType.StoredProcedure, "PAS_ADICIONAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD", parameters);
					}
				}
			}
			catch (SqlException sqlEx)
			{
				throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "PAS_ADICIONAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD"), sqlEx);
			}
			catch (Exception ex)
			{
				throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorInsertar", "PAS_ADICIONAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD"), ex);
			}
		}

		public void EliminarAreasArtisticasFormacionEntidad(decimal parENT_ID, params SqlTransaction[] transaccion)
		{
			try
			{
				SqlParameter param1 = new SqlParameter(ENT_ID_PARAM, parENT_ID);
				SqlParameter[] parameters = {param1};
				if (transaccion.Length == 0)
					SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD_ENT_ID", parameters);
				else
					SqlHelper.ExecuteNonQuery(transaccion[0], CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD_ENT_ID", parameters);
			}
			catch (SqlException sqlEx)
			{
				throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "PAS_ELIMINAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD_ENT_ID o PAS_ELIMINAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD_ENT_ID"), sqlEx);
			}
			catch (Exception ex)
			{
				throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorEliminar", "PAS_ELIMINAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD_ENT_ID o PAS_ELIMINAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD_ENT_ID"), ex);
			}
		}

		public void EliminarAreasArtisticasFormacionEntidad(ArrayList arrIDs)
		{
			SqlConnection objConnection = new SqlConnection(ConnectionString);
			objConnection.Open();
			SqlTransaction objTrans = objConnection.BeginTransaction();

			try
			{
				foreach (object Item in arrIDs)
				{
					SqlParameter param1 = new SqlParameter(ENT_ID_PARAM, decimal.Parse(Item.ToString()));
					SqlParameter param2 = new SqlParameter(ARE_ID_PARAM, decimal.Parse(Item.ToString()));
					SqlParameter[] parameters = {param1, param2};
					SqlHelper.ExecuteNonQuery(objTrans, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD", parameters);
				}
				objTrans.Commit();
			}
			catch (SqlException sqlEx)
			{
				objTrans.Rollback();
				throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "PAS_ELIMINAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD"), sqlEx);
			}
			catch (Exception ex)
			{
				objTrans.Rollback();
				throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorEliminar", "PAS_ELIMINAR_ART_AREAS_ARTISTICAS_FORMACION_ENTIDAD"), ex);
			}
			finally
			{
				objConnection.Close();
			}
		}
	}
}